package Servlets.yz;

import Bean.Admin;
import Bean.User;
import Util.GokJDBCUtil2;
import Util.SlatMD5Util;
import com.oracle.wls.shaded.org.apache.xpath.operations.Bool;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.text.StyledEditorKit;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

import static Util.SlatMD5Util.verifySaltPassword;


/*
* 用于实现用户和管理员登录功能
* */
@WebServlet(name="LoginServlet",urlPatterns = "/LoginServlet")

/*@MultipartConfig*///上传文件用的注解
public class LoginServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(LoginServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sqlVerifyUse = "select * from user where username=?";
        String sqlVerifyAdmin = "select * from admin where adminaccount=?";
        User user = GokJDBCUtil2.queryForObject(sqlVerifyUse, User.class, username);
        Admin admin = GokJDBCUtil2.queryForObject(sqlVerifyAdmin, Admin.class, username);
        if (admin != null){
            //检查输入的密码和数据库中的密码是否相同
            Boolean passwordVerify2 = SlatMD5Util.verifySaltPassword(password,admin.getAdminpassword());
            if(passwordVerify2){
                HttpSession adminSession = req.getSession();
                adminSession.setAttribute("admin",admin.getAdminstatus());
                LocalDateTime updateTime = LocalDateTime.now();//获取现在的时间
                String sql = "update admin set updatetime=? where adminaccount=?";
                GokJDBCUtil2.update(sql,updateTime,username);
                req.getRequestDispatcher("pages/adminIndex.jsp").forward(req,resp);
            }
            else {
                resp.sendRedirect(req.getContextPath()+"/pages/userLogin.jsp");
            }
        }
        else if(user != null){
            //检查输入的密码和数据库中的密码是否相同
            Boolean passwordVerify1 = SlatMD5Util.verifySaltPassword(password,user.getUserpassword());
            if(passwordVerify1){
                HttpSession session = req.getSession();
                session.setAttribute("admin",0);
                session.setAttribute("user",user);
                log.info("user:"+user);
                LocalDateTime updatetime = LocalDateTime.now();
                String sql = "update user set updatetime=? where username=?";
                GokJDBCUtil2.update(sql,updatetime,username);
                req.getRequestDispatcher("UserIndexServlet").forward(req,resp);
            }

            else {
                resp.sendRedirect(req.getContextPath()+"/pages/userLogin.jsp");
            }
        }


        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
